package com.vonchange.purchase.purchase.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vonchange.headb.core.map.HeaMap;
import com.vonchange.headb.core.page.IPage;
import com.vonchange.purchase.purchase.bean.Purchase;
import com.vonchange.purchase.purchase.service.IPurchaseLogService;
import com.vonchange.purchase.purchase.service.IPurchaseService;
import com.vonchange.utils.page.PageUtils;
import com.vonchange.utils.web.RequestUtils;
import com.vonchange.utils.web.Result;
@Controller
@RequestMapping("/purchase")
public class PurchaseAction {
	@Resource
	private IPurchaseService purchaseService;
	@Resource
	private IPurchaseLogService purchaseLogService;
	@RequestMapping("/export")
	@ResponseBody
	public Result export(@RequestParam("deedId")String deedId, HttpServletRequest request) throws IOException{
		Result result=purchaseService.export(deedId);
		return result;
	}

	@RequestMapping("/findById")
	@ResponseBody
	public Purchase findById(@RequestParam("id")String id){
		return purchaseService.findById(id);
	}
	@RequestMapping("/findList")
	@ResponseBody
	public List<Purchase> findList(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> param =RequestUtils.getQueryParams(request);
		return purchaseService.findList(new HeaMap().setAll(param));
	}
	@RequestMapping("/findPage")
	@ResponseBody
	public IPage<Purchase> findPage(Integer pageNo,Integer pageSize,HttpServletRequest request,HttpServletResponse response){
		IPage<Purchase> page=PageUtils.initPage(pageNo, pageSize);
		Map<String, Object> param =RequestUtils.getQueryParams(request);
		return purchaseService.findPage(page,new HeaMap().setAll(param));
	}
	@RequestMapping("/delete")
	@ResponseBody
	public Result deleteById(@RequestParam("id")String id){
		int result=purchaseService.deleteById(id);
		if(result==0){
			return new Result().setStatusFail().setMsg("删除失败");
		}
		return new Result().setMsg("删除成功");
	}
	@RequestMapping("/add")
	@ResponseBody
	public Result add(Purchase purchase,HttpServletRequest request,HttpServletResponse response){
		if(null==purchase){
			return new Result().setStatusFail().setMsg("请传入数据");
		}
		String msg="增加";
		purchase =purchaseService.save(purchase);
		return new Result().setResult(purchase).setMsg(msg+"成功");
	}   
	@RequestMapping("/update")
	@ResponseBody
	public Result update(Purchase purchase,HttpServletRequest request,HttpServletResponse response){
		if(null==purchase){
			return new Result().setStatusFail().setMsg("请传入数据");
		}
		String msg="修改";
		if(null==purchase.getId()){
			return new Result().setStatusFail().setMsg("请输入ID");
		}
		int result=purchaseService.update(purchase);
		if(result<=0){
			return new Result().setStatusFail().setMsg(msg+"未成功");
		}
		return new Result().setResult(purchase).setMsg(msg+"成功");
	}     
	/*	@RequestMapping("/export")
	@ResponseBody
	public Result export(@RequestParam("pListJson")String pListJson,
			@RequestParam("logJson")String logJson,@RequestParam("deedId")String deedId, HttpServletRequest request){
		System.out.println(pListJson);
		System.out.println(logJson);
		List<Purchase> purchaseList=JsonUtil.fromJson(pListJson, new TypeReference<List<Purchase>>() {
		});
		//存
		purchaseLogService.saveJsonList(deedId,logJson);
		String webPath=purchaseService.export(deedId,purchaseList);
		return new Result().setResult(webPath);
	}*/

}
